import { defineStore } from 'pinia'
import request from '@/utils/request'
import { Profile } from '@/types/user'
import { ApiRes } from '@/types/data'
import { setUserInfo,removeUserInfo} from '@/utils/storeage'
export default defineStore('user', {
  state() {
    return {
      userInfo: {} as Profile
    }
  },
  actions: {
    // 登录
    async loginByAccount(data: { account: string; password: string }) {
      const res = await request.post<ApiRes<Profile>>('/login', data)
      this.userInfo = res.data.result
      setUserInfo(res.data.result)
    },
    // 填写手机号 获取验证码
    async getCode(mobile:string){
    await request.get('/login/code',{params:{mobile}})
    },
    // 手机加验证码登录
    async loginByCode(data:{mobile:string,code:string}){
      const res = await request.post<ApiRes<Profile>>('/login/code',data)
      this.userInfo=res.data.result
      setUserInfo(res.data.result)
    },
    //退出登录
    logout(){
      // 清除userinfo数据与本地储存
      this.userInfo={} as Profile
      removeUserInfo()
    }
  },
  persist:{
    storage:localStorage,
    paths:['userInfo']
  }
})
